import {message, Modal, notification, Form, FormItem, Input} from 'ant-design-vue';
import {ExclamationCircleOutlined, CheckCircleOutlined} from '@ant-design/icons-vue';
import {createVNode, h} from 'vue';
import useLoadingStore from '@/store/modules/loading'

export default {
  // 消息提示
  msg(content) {
    message.info(content)
  },
  // 错误消息
  msgError(content) {
    message.error(content)
  },
  // 成功消息
  msgSuccess(content) {
    message.success(content)
  },
  // 警告消息
  msgWarning(content) {
    message.warning(content)
  },
  // 弹出提示
  alert(content) {
    Modal.confirm({
      title: '系统提示',
      icon: createVNode(ExclamationCircleOutlined, {style: "color: var(--ant-info-color)"}),
      content: content,
    })
  },
  // 错误提示
  alertError(content) {
    Modal.confirm({
      title: '系统提示',
      icon: createVNode(ExclamationCircleOutlined, {style: "color: var(--ant-error-color)"}),
      content: content,
    })
  },
  // 成功提示
  alertSuccess(content) {
    Modal.confirm({
      title: '系统提示',
      icon: createVNode(CheckCircleOutlined, {style: "color: var(--ant-success-color)"}),
      content: content,
    })
  },
  // 警告提示
  alertWarning(content) {
    Modal.confirm({
      title: '系统提示',
      icon: createVNode(ExclamationCircleOutlined),
      content: content,
    })
  },
  // 通知提示
  notify(description) {
    notification.info({
      description
    })
  },
  // 错误通知
  notifyError(description) {
    notification.error({
      description
    });
  },
  // 成功通知
  notifySuccess(description) {
    notification.success({
      description
    })
  },
  // 警告通知
  notifyWarning(description) {
    notification.warning({
      description
    })
  },
  // 确认窗体,返回promise可以使窗体loading
  confirm(content) {
    return new Promise((resolve, reject) => {
      Modal.confirm({
        title: '系统提示',
        icon: createVNode(ExclamationCircleOutlined),
        content: content,
        okText: '确定',
        cancelText: '取消',
        onOk() {
          return resolve()
        },
        onCancel() {
          reject()
        },
      })
    })
  },
  // 提交内容
  prompt(tip, title, props) {
    const form = reactive({
      input: undefined
    })
    const formRef = ref(null)
    return Modal.confirm({
      title: title || '系统提示',
      okText: props.confirmButtonText,
      cancelText: props.cancelButtonText,
      maskClosable: props.closeOnClickModal,
      content: () => h(Form, {
        model: form,
        ref: formRef,
        layout: 'vertical',
        rules: {
          input: [{pattern: props.inputPattern, message: props.inputErrorMessage, trigger: "change"}],
        }
      }, h(FormItem, {
        label: tip,
        name: 'input'
      }, h(Input, {
        value: form.input,
        'onUpdate:value': (value) => form.input = value
      }))),
      onOk() {
        return new Promise((resolve0, reject0) => {
          formRef.value.validate().then(async () => {
            await props?.onOk({value: form.input})
            resolve0()
          }).catch((errorInfo) => reject0(errorInfo))
        })
      }
    })
  },
  // 打开遮罩层
  loading(content) {
    const loadingStore = useLoadingStore();
    const {loading, tip} = storeToRefs(loadingStore);
    tip.value = content
    loading.value = true
  },
  // 关闭遮罩层
  closeLoading() {
    const loadingStore = useLoadingStore();
    const {loading, tip} = storeToRefs(loadingStore);
    tip.value = null
    loading.value = false
  }
}
